#coding: utf-8
"""
    file name: TulingRobot.py
    platform: Windows,Linux
    description: 使用Tuling服务实现机器人问答

    module author: Guangwei Jiang (Guangwei_Jiang@wistron.com)
    Create Date: 2020/9/20

"""
import requests
import json

class TulingRobot:
    def __init__(self):
        try:
            #图灵机器，配置信息
            self.tulingUrl='http://openapi.tuling123.com/openapi/api/v2'
            self.turingApiKey = "d8cd9000c38148309ec1a69e19eb14e7"#图灵机器人api_key
        except Exception as e:
            print(e)
        
    def response(self, usersay="你好"):
        """Function to get response from Tuling Robot

        |  Args:
        |  1. usersay: The input words need Tuling response

        |  Return -- The response string from Tuling Robot

        >>> # Get response from Tuling Robot
        >>> tulingResponse = response("苏州的天气怎么样？")
        """
        result = '没有明白你的问题'
        try:
            robot = {
                "perception": {
                    "inputText": {
                    "text": usersay
                    }
                },
                "userInfo": {
                    "apiKey": self.turingApiKey,
                    "userId": 'Dudu'
                }
            }
            response=json.loads(requests.post(self.tulingUrl,None,robot,headers={'Content-Type':'charset=UTF-8'}).text)
            result = str(response['results'][0]['values']['text'])
        except Exception as e:
            print(e)
        finally:
            return result
    
if __name__ == "__main__":
    tuling = TulingRobot()
    response = tuling.response("苏州的天气怎么样？")
    print(response)
